package com.example.demo.utils;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.UndeclaredThrowableException;

public final class ExceptionUtil {
    private ExceptionUtil() {
    }

    /**
     * 输出异常堆栈
     */
    public static String getStackTrace(Throwable thr) {
        StringWriter sw = new StringWriter();

        try (PrintWriter pw = new PrintWriter(sw)) {
            thr.printStackTrace(pw);
        }

        return sw.toString();
    }

    /**
     * 获得完整消息，包括异常名
     */
    public static String getMessage(Throwable e) {
        return String.format("%s: %s", e.getClass().getSimpleName(), e.getMessage());
    }

}
